package com.ssau.expert.gui.master;

import com.ssau.expert.gui.common.Controller;
import com.ssau.expert.gui.common.MyControls;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

/**
 * Author: Sergey Saiyan
 * Created: 05.09.13 11:51
 */
public class ExpertFrame extends JFrame implements ExpertPresenter {

    private static final long serialVersionUID = -3985338753569078946L;

    private static final String NAME = "Expert System v1.0";

    @Override
    public void init() {
        this.setLayout(null);
        initComponents();
        initDisposition();

        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                Controller.get().showLoginFrame();
            }
        });
    }

    @Override
    public void showFrame() {
        setName(NAME);
        setTitle(NAME);
        setVisible(true);
    }

    @Override
    public void displayUserInput(String shipProfileAnswer, String staffAnswer, String distanceAnswer) {
        profileJTextField.setText(shipProfileAnswer);
        autonomyJTextField.setText(distanceAnswer);
        personalJTextField.setText(staffAnswer);
    }

    @Override
    public void showMessageDialog(String message) {
        JOptionPane.showMessageDialog(this, message);
    }

    @Override
    public JFrame asFrame() {
        return this;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void displayRules(List<String> rules) {
        rulesListModel.clear();

        for (String rule : rules) {
            rulesListModel.addElement(rule);
        }
    }

    @Override
    public void clearResultListModel() {
        resultListModel.clear();
    }

    @SuppressWarnings("unchecked")
    @Override
    public void addElementToResultList(String element) {
        resultListModel.addElement(element);
    }

    @Override
    public String showInputDialog(String question) {
        return JOptionPane.showInputDialog(this, question);
    }

    @Override
    public void enableStartConsultation(boolean enable) {

    }


    private void initComponents() {

        JPanel rulesPanel = MyControls.CreateJPanel(this, 0, 0, 800, FORM_SIZE.height - 40, "Правила");
        rulesListModel = MyControls.CreateJList(rulesPanel, 10, 15, rulesPanel.getWidth() - 20, rulesPanel.getHeight() - 25);

        JTable table = MyControls.fillExpertTable(0, 0, 400, 400);
        table.setVisible(true);

        JPanel p = new JPanel();
        p.setSize(400, 400);
        p.setLocation(800, 0);

        //getContentPane().add(jscrlp);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        p.add(table.getTableHeader(), BorderLayout.NORTH);
        p.add(table, BorderLayout.CENTER);

        add(p);

        JButton getRulesJButton = MyControls.createButton("Загрузить правила",
                rulesPanel.getWidth(), rulesPanel.getHeight() - 30);

        add(getRulesJButton);
        getRulesJButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getRules();
            }
        });
    }

    private void initDisposition() {
        //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(FORM_SIZE);
        setLocationRelativeTo(null);

    }

    private static ExpertFrame instance;

    private void getRules() {
        Controller.get().uploadRules();
    }

    private void startConsultation() {
        Controller.get().startConsultation();
    }

    public static ExpertFrame get() {
        if (instance == null) {
            instance = new ExpertFrame();
        }
        return instance;
    }

    private ExpertFrame() {
    }

    private DefaultListModel rulesListModel;
    private DefaultListModel resultListModel;

    private Dimension FORM_SIZE = new Dimension(1220, 550);
    private JTextField profileJTextField;
    private JTextField autonomyJTextField;
    private JTextField personalJTextField;
    private JButton consultationJButton;
}
